iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 16
0
影片教學

懶人寫寫 BOT系列 第 16

Day 16

  • 分享至 

  • xImage
  •  

嗨,我是 Fly,用 Ruby 寫 Chatbot 並挑戰30天分享心得
為確保不會沒靈感
每日含 Ruby 解題增加內容
https://www.hackerrank.com/domains/tutorials/30-days-of-code

Day 18: Queues and Stacks

# create the Solution class
class Solution
  def initialize
    @stack =''
    @queue=''
  end

    def push_character c
      @stack += c if c!="\n"
    end

    def enqueue_character c
      @queue += c if c!="\n"
    end

    def dequeue_character
      @queue.reverse
    end

    def pop_character
      @stack
    end
end


# create Solution class object
solution = Solution.new

# read the input
input = gets

input.split('').each do |c|
    # push the character to stack
    solution.push_character c

    # enqueue the character to queue
    solution.enqueue_character c
end

# check if input string is palindrome or not
is_palindrome = true

(input.length / 2).times do
    if solution.pop_character != solution.dequeue_character
        is_palindrome = false
        break
    end
end

# print if string is palindrome or not
if is_palindrome
    puts "The word, #{input}, is a palindrome."
else
    puts "The word, #{input}, is not a palindrome."
end

上一篇
Day 15
下一篇
Day 17 用 Line Notify 做一個元氣彈書籤
系列文
懶人寫寫 BOT30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言